package GUI;

import java.util.ArrayList;

import processing.core.PImage;


public class MultistateButton extends Component
{
    public boolean clicked;
    public boolean pressed;
    public int curState;
    public ArrayList states;

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public MultistateButton()
    {
        width = 22;
        height = 22;
        states = new ArrayList();
    }

    public void setIcon(String name)
    {
        if(name != null)
        {
          icon = Utils.globalProcessing.loadImage(name);
        }
    }
  
    public void draw()
    {
        clicked = false;
        int cx = x;
        int cy = y;
        if(mouseOver()) 
        {
            super.draw();
            if(Utils.globalProcessing.mousePressed)
            {
                pressed = true;
                cy+=2;
            }
            else if(pressed)
            {
                pressed = false;
                clicked = true;
                
                // Switch state.
                curState++;
                if(curState >= states.size()) curState = 0;
            }
        }
        else
        {
            pressed = false;
        }
        if(icon != null)
        {
        	Utils.globalProcessing.image(icon, cx, cy);
        }
        if(states.size() > 0)
        {
            String txt = (String)states.get(curState);
            Utils.globalProcessing.textFont(Utils.plotFont, 16);
            Utils.globalProcessing.textAlign(Utils.globalProcessing.LEFT);
            Utils.globalProcessing.fill(30);
            Utils.globalProcessing.text(txt, cx + 25, cy + 16);
            width = (int) (Utils.globalProcessing.textWidth(txt) + 28);
        }
    }
  
  ////////////////////////////////////////////////////////////////////////////////////////////////////
  PImage icon;
}